{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook reproduces a [visualization by the Wall Street Journal](http://graphics.wsj.com/infectious-diseases-and-vaccines/#b02g20t20w15) about the incidence of measles over time, which the brilliant [Brian Granger](https://github.com/ellisonbg) adapted into an [example for the Altair library](http://nbviewer.jupyter.org/github/ellisonbg/altair/blob/master/altair/notebooks/12-Measles.ipynb).\n",
    "\n",
    "Most examples work across multiple plotting backends, this example is also available for:\n",
    "\n",
    "* [Bokeh measles_example](../bokeh/measles_example.ipynb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import holoviews as hv\n",
    "import pandas as pd\n",
    "from holoviews import opts\n",
    "\n",
    "hv.extension('matplotlib')\n",
    "hv.output(fig='svg')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Declaring data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "url = 'https://raw.githubusercontent.com/blmoore/blogR/master/data/measles_incidence.csv'\n",
    "data = pd.read_csv(url, skiprows=2, na_values='-')\n",
    "\n",
    "yearly_data = data.drop('WEEK', axis=1).groupby('YEAR').sum().reset_index()\n",
    "measles = pd.melt(yearly_data, id_vars=['YEAR'], var_name='State', value_name='Incidence')\n",
    "\n",
    "heatmap = hv.HeatMap(measles, label='Measles Incidence')\n",
    "aggregate = hv.Dataset(heatmap).aggregate('YEAR', np.mean, np.std)\n",
    "\n",
    "vline = hv.VLine(1963)\n",
    "marker = hv.Text(1964, 800, 'Vaccine introduction', halign='left')\n",
    "\n",
    "agg = hv.ErrorBars(aggregate) * hv.Curve(aggregate)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "(heatmap + agg * vline * marker).opts(\n",
    "    opts.HeatMap(aspect=1.4,invert_yaxis=True, show_values=False, show_frame=False,\n",
    "                 labelled=[], xaxis=None, logz=True),\n",
    "    opts.Layout(aspect_weight=1, fig_size=300, vspace=0, sublabel_format=None),\n",
    "    opts.Overlay(aspect=3, show_title=False, bgcolor='white', \n",
    "                 show_legend=False, show_frame=False, xrotation=90),\n",
    "    opts.VLine(color='black', ylim=(0, 1050))).cols(1)"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
